var util = require("../../utils/util");
var config = require("../../config");
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: 0,
    chat_msg: [],
    msg: "",
    cs_id: "",
    user_receiver: "",
    user_opposite: "",
    user_opposite_name: "",
    scroll:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var chat_session_id = options.chat_session_id;
    var user_receiver = options.user_opposite;
    var user_opposite = options.user_opposite;
    var user_opposite_name = options.user_opposite_name;
    that.setData({
      userInfo: app.globalData.userInfo,
      cs_id:chat_session_id,
      user_receiver: user_receiver,
      user_opposite: user_opposite,
      user_opposite_name: user_opposite_name
    });

    wx.setNavigationBarTitle({
      title: user_opposite_name,
    })

    wx.request({
      url: config.urls.oneChatsessionMsgs,
      data: {
        my_id: app.globalData.userInfo.id,
        cs_id: chat_session_id,
        ps:20,
        pn:0
      },
      success: function (res) {
        if (res.data.success) {
          console.log(res)
          that.formatData(res.data.msgs)
          // var id = "scroll" + res.data.msgs[res.data.msgs.length-1].id
          that.setData({
            chat_msg: res.data.msgs.reverse(),
            scroll:"abc"
          })
        }
        else {
          console.log("no this chatmsg info: ", book_id);
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.request({
      url: config.urls.oneChatsessionMsgs,
      data: {
        my_id: app.globalData.userInfo.id,
        cs_id: that.data.cs_id,
        ps: 10,
        pn: 0
      },
      success: function (res) {
        if (res.data.success) {
          console.log(res)
          that.formatData(res.data.msgs)
          // var id = "scroll" + res.data.msgs[res.data.msgs.length-1].id
          that.setData({
            chat_msg: res.data.msgs.reverse(),
            scroll: "bcd"
          })
        }
        else {
          console.log("no this chatmsg info: ", book_id);
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  getMsg: function (e) {
    var that = this;
    that.setData({
        msg: e.detail.value,
      })

  },

  send_msg:function () {
    var that  = this;
    if(that.data.msg != '') {
      wx.request({
        url: config.urls.send_chat_msg,
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          content:that.data.msg,
          cs_id: that.data.cs_id,
          //发送方
          user_sender: app.globalData.userInfo.id,
          //接收方
          user_receiver: that.data.user_receiver
        },
        success: function (res) {
          if (res.data.success) {
            that.formatData1(res.data.msg)
            that.data.chat_msg.push(res.data.msg);
            console.log(res.data.msg)
            var random = that.random()
            console.log(random)
            that.setData({
              chat_msg:that.data.chat_msg,
              msg:"",
              scroll:random
            })
          }
          else {
            console.log("send msg failed.");
          }
        }
      });
    }
  },
  // send_msg:function(e) {
  //   var that = this
  //   wx.navigateTo({
  //     url: "/pages/sendmsg/sendmsg?user_sender=" + app.globalData.userInfo.id + "&user_receiver=" + that.data.user_receiver + "&cs_id=" + that.data.cs_id
  //   })
  // },

  formatData: function (data) {
    for (var i = 0; i < data.length; i++) {
      var item = data[i];
      item.created_time = util.ftime(item.created_time);
    }
  },
  formatData1:function(e){
    e.created_time = util.ftime(e.created_time);
  },
  random: function() {
    var i = 1;
    var result = "";
    for (i = 1; i <= 4; i++) {
      var strRadom = Math.floor(Math.random() * 26) + 1;
      result = result + String.fromCharCode(65 + strRadom)
    }
    return result;
  }
})